﻿//https://leetcode.cn/problems/search-insert-position/?envType=study-plan-v2&envId=top-100-liked

class Solution {
public:
    int searchInsert(vector<int>& nums, int target)
    {
        if (target <= nums[0])
        {
            return 0;
        }
        if (target > nums[nums.size() - 1])
        {
            return nums.size();
        }

        int left = 0, right = nums.size() - 1;
        while (left <= right)
        {
            int mid = (left + right) / 2;
            if (target == nums[mid])
            {
                return mid;
            }
            else if (target < nums[mid])
            {
                right = mid - 1;
            }
            else
                left = mid + 1;
        }

        return left;
    }
};